package com.itheima.youdianda.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.youdianda.common.PageBean;
import com.itheima.youdianda.common.R;
import com.itheima.youdianda.common.ResultData;
import com.itheima.youdianda.dto.YddAdvimgDto;
import com.itheima.youdianda.entity.YddAdvimg;
import com.itheima.youdianda.entity.YddAdvpos;
import com.itheima.youdianda.service.YddAdvimgService;
import com.itheima.youdianda.service.YddAdvposService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/ydd_advimg")
@Slf4j
public class YddAdvimgController {
    @Autowired
    private YddAdvimgService yddAdvimgService;
    @Autowired
    private YddAdvposService yddAdvposService;

    /**
     * 广告图管理分页查询
     * @param advimgdesc
     * @param advimgpos
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public ResultData list(String advimgdesc, String advimgpos, int page, int limit){
        //分页构造器
        Page<YddAdvimg> yddAdvimgPage = new Page<>(page, limit);
        //创建条件构造器
        LambdaQueryWrapper<YddAdvimg> yddAdvimgLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件,模糊查询
        yddAdvimgLambdaQueryWrapper.like(StringUtils.isNotEmpty(advimgdesc),YddAdvimg::getAdvimgdesc,advimgdesc).
        eq(StringUtils.isNotEmpty(advimgpos),YddAdvimg::getAdvimgpos,advimgpos);

        //添加排序条件
        yddAdvimgLambdaQueryWrapper.orderByDesc(YddAdvimg::getId);
        //查询
        Page<YddAdvimg> yddAdvimgPageInfo = yddAdvimgService.page(yddAdvimgPage, yddAdvimgLambdaQueryWrapper);

        //dto分页构造
        Page<YddAdvimgDto> yddAdvimgDtoPage = new Page<>();

        //对象拷贝
        BeanUtils.copyProperties(yddAdvimgPageInfo,yddAdvimgDtoPage,"records");
        List<YddAdvimg> yddAdvimgPageInfoRecords = yddAdvimgPageInfo.getRecords();
        List<YddAdvimgDto> yddAdvimgDtoList = new ArrayList<>();
        for (YddAdvimg record : yddAdvimgPageInfoRecords) {

            YddAdvimgDto yddAdvimgDto = new YddAdvimgDto();
            BeanUtils.copyProperties(record,yddAdvimgDto);
            //获取"广告图位置"，关联广告位id
            String recordAdvimgpos = record.getAdvimgpos();

            //根据id查询分类对象
            YddAdvpos yddAdvpos = yddAdvposService.getById(Integer.parseInt(recordAdvimgpos));

            if (yddAdvpos != null){
                yddAdvimgDto.setAdvposname(yddAdvpos.getAdvposname());
            }
            yddAdvimgDtoList.add(yddAdvimgDto);
        }
        yddAdvimgDtoPage.setRecords(yddAdvimgDtoList);

        //创建分页对象
        PageBean<YddAdvimgDto> yddAdvimgDtoPageBean = new PageBean<>();
        //设置总条数
        yddAdvimgDtoPageBean.setCount((int) yddAdvimgDtoPage.getTotal());
        //设置当前页码
        yddAdvimgDtoPageBean.setCurrentPage((int) yddAdvimgDtoPage.getCurrent());
        //设置每页显示条数
        yddAdvimgDtoPageBean.setPageSize((int) yddAdvimgDtoPage.getSize());
        //设置总页数
        yddAdvimgDtoPageBean.setTotalPages((int) yddAdvimgDtoPage.getPages());
        //设置data
        yddAdvimgDtoPageBean.setData(yddAdvimgDtoPage.getRecords());

        return ResultData.ok("",yddAdvimgDtoPageBean);

    }

    /**
     * 广告图管理数据回显
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(String id) {
        log.info("回显的标签id为:{}", id);
        //根据id查找
        YddAdvimg advimg = yddAdvimgService.getById(id);
        return ResultData.ok("", advimg);
    }

    /**
     * 广告图管理修改
     * @param yddAdvimg
     * @return
     */
    @PostMapping("/update")
    public R update(@RequestBody YddAdvimg yddAdvimg) {
        log.info("yddAdvimg:{}", yddAdvimg);
        yddAdvimgService.updateById(yddAdvimg);
        return R.success("", "");
    }

    /**
     * 广告图管理删除
     * @param id
     * @return
     */
    @GetMapping("/del")
    public R del(String id) {
        log.info("要删除的标签id为:{}", id);
        //根据id删除
        yddAdvimgService.removeById(id);
        return R.success("", "");
    }

    /**
     * 广告图管理新增
     * @param yddAdvimg
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody YddAdvimg yddAdvimg) {
        log.info("yddAdvimg:{}", yddAdvimg);
        yddAdvimgService.save(yddAdvimg);
        return R.success("", "");
    }

}
